package com.coolbi.security.biz.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.security.biz.RoleToGroupBiz;
import com.coolbi.security.dao.RoleToGroupDao;
import com.coolbi.security.entity.RoleToGroup;
import com.coolbi.security.entity.Roles;
import com.coolbi.security.entity.UserGroup;

public class RoleToGroupBizImpl implements RoleToGroupBiz {

	@Autowired
	private RoleToGroupDao roleToGroupDao;
	public void setRoleToGroupDao(RoleToGroupDao roleToGroupDao) {
		this.roleToGroupDao = roleToGroupDao;
	}
	
	@Override
	public List<RoleToGroup> find(Map<String, Object> parameter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleToGroup> find(Map<String, Object> parameter, int offset,
			int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count(Map<String, Object> parameter) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int countByKeyword(String keyword) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<RoleToGroup> search(String keyword, int offset, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public RoleToGroup save(RoleToGroup obj) {
		return roleToGroupDao.save(obj);
	}

	@Override
	public int update(RoleToGroup obj) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete(long id) {
		return roleToGroupDao.delete(id);
	}

	@Override
	public RoleToGroup get(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleToGroup> list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleToGroup> list(int offset, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Roles> findRoleByGroupId(long id) {
		return roleToGroupDao.findRoleByGroupId(id);
	}

	@Override
	public List<UserGroup> findGroupByRoleId(long id) {
		return roleToGroupDao.findGroupByRoleId(id);
	}

	@Override
	public int deleteByGroupId(long id) {
		return roleToGroupDao.deleteByGroupId(id);
	}

	@Override
	public List<Roles> findParentRoleByGroupId(long id) {
		return roleToGroupDao.findParentRoleByGroupId(id);
	}

	@Override
	public void saveRoleAuthToGroup(Long[] authArray, Long[] abolishArray,
			RoleToGroup roleToGroup) {
		if (null != abolishArray) {
			roleToGroupDao.deleteByRoleIds(abolishArray, roleToGroup.getGroupId());
		}
		if (null != authArray) {
			List<Long> roleIds = roleToGroupDao.findByRolesAndGroupId(authArray, roleToGroup.getGroupId());
			for (int i = 0; i < authArray.length; i ++) {
				if (roleIds.size() > 0) {
					if (!roleIds.contains(authArray[i])) {
						roleToGroup.setRoleId(authArray[i]);
						roleToGroupDao.save(roleToGroup);
					}
				} else {
					roleToGroup.setRoleId(authArray[i]);
					roleToGroupDao.save(roleToGroup);
				}
			}
		}
	}

}
